package com.bawei.controller;


import com.bawei.pojo.Notice;
import com.bawei.pojo.UserInfo;
import com.bawei.service.NoticeService;
import com.bawei.util.JsonUtils;
import com.bawei.util.PageView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("notice")
public class NoticeController {



    @Autowired
    private NoticeService noticeService;

    @RequestMapping("page")
    public String page(Model model){
        List<UserInfo> list=noticeService.selectUsers();
        model.addAttribute("users", list);
        return "forward:/system/noticeAdd.jsp";
    }

    /**
     *
     * @param notice
     * @param recipinet 接收人
     * @param file   文件
     * @return
     */
    @RequestMapping("save")
    public String save(Notice notice, String[] recipinet, @RequestParam(required = false) MultipartFile[] file, HttpSession session, HttpServletRequest request){
        UserInfo userInfo=(UserInfo) session.getAttribute("user");
        noticeService.save(notice,recipinet,file,userInfo,request);
        return "forward:/system/noticeMy.jsp";

    }
    @RequestMapping("Mypublish")
    @ResponseBody
    public Map<String,Object> myPublish(HttpSession session, @RequestParam(defaultValue = "1") Integer currentPage){
        UserInfo userInfo=(UserInfo) session.getAttribute("user");
        PageView<Notice> pageView=noticeService.myPublish(userInfo.getId(),currentPage);
        HashMap<String, Object> map = new HashMap<>();
        if (pageView.getRecords().size() > 0) {
            String json=JsonUtils.objectToJson(pageView);
            map.put("page", json);
            map.put("notices", pageView.getRecords());
            map.put("msg", "");
        } else {
            map.put("msg", "您还没有发不过通知公告呢");
        }
        return map;
    }
}
